package qnb.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import qnb.dao.*;
import qnb.entity.Room;
import qnb.entity.Student;
import qnb.service.StudentService;

@Service
public class StudentServiceImpl implements StudentService {
	@Autowired
	StudentDao dao;
	@Autowired
	StudentStyleDao studentStyleDao;
	@Autowired
	GradeMapper gradeDao;
	@Autowired
	SubjectDao subjectDao;
	@Autowired
	RoomDao roomDao;
	@Autowired
	TeacherDao teacherDao;
	@Override
	public int addStudent(Student stu) {
		return dao.addStudent(stu);
	}

	@Override
	public int updateStudent(Student stu) {
		// TODO Auto-generated method stub
		return dao.updateStudent(stu);
	}

	@Override
	public int deleteStudent(String studentId) {
		// TODO Auto-generated method stub
		return dao.deleteStudent(studentId);
	}

	@Override
	public Student selectOne(String studentId) {
		Student stu = dao.selectOne(studentId);
		if(stu != null){
			return Fill(stu);
		}else{
			return null;
		}
	}

	@Override
	public List<Student> selectPage(String studentName,String oderby, int page, int pageSize) {
		List<Student> ls = dao.selectPage(studentName,oderby, page, pageSize);
		for(Student stu : ls) {
			stu = Fill(stu);
		}
		return ls;
	}

	//计数用以分页
	@Override
	public int count(String where){
		return dao.count(where);
	}

	//填充数据
	private Student Fill(Student stu){
		if(stu.getStudentStyleId() != null && !"".equals(stu.getStudentStyleId())) {
			stu.setStudentStyleName(studentStyleDao.selectOne(stu.getStudentStyleId()).getStudentStyleName());
		}
		if(stu.getGradeId() != null && !"".equals(stu.getGradeId())) {
			stu.setGradeName(gradeDao.selectGradeById(stu.getGradeId()).getGradeName());
		}
		if(stu.getSubjectId() != null && !"".equals(stu.getSubjectId())) {
			stu.setSubjectName(subjectDao.selectOne(stu.getSubjectId()).getSubjectName());
		}
		if(stu.getRoomId() != null && !"".equals(stu.getRoomId())) {
			Room room = roomDao.selectToId(stu.getRoomId());
			System.out.println(room);
			stu.setRoomName(room.getRoomName());

		}
		if(stu.getTeacherId() != null && !"".equals(stu.getTeacherId())){
			stu.setTeacherName(teacherDao.selectOne(stu.getTeacherId()).getTeacherName());
		}
		return stu;
	}

	@Override
	public List<Student> selectToDown(String where) {
		List<Student> ls = dao.selectToDown(where);
		for(Student stu : ls) {
			stu = Fill(stu);
		}
		return ls;
	}

	@Override
	public List<Student> selectAll() {
		return dao.selectAll();
	}

	public Student login(String studentAccount,String studentPassword){
		Student student = dao.login(studentAccount,studentPassword);
		if (student == null){
			return null;
		}else{
			return student;
		}
	}
	/**
	 * 判断账号是否重复
	 */
	@Override
	public int selectOneForAccount(String account){
		int count = dao.selectOneForAccount(account);
		if (count>0){
			return count;
		}
		return teacherDao.checkAccount(account);
	}

	@Override
	public int addExcelData(List<Student> list) {
		// TODO Auto-generated method stub
		return dao.addExcelData(list);
	}

	@Override
	public List<Student> checkExcel(List<String> studentId) {
		// TODO Auto-generated method stub
		return dao.checkExcel(studentId);
	}

	@Override
	public List<Student> checkExcel2(List<String> studentAccount) {
		// TODO Auto-generated method stub
		return dao.checkExcel2(studentAccount);
	}
}
